﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class ChartScoreKnowModle : System.Web.UI.Page
    {
        protected int examroomid = 0, studentid = 0;
        protected string mychart = "", classchart = "", gradechart = "", areachart = "", allchart = "";
        protected bool isquery = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                if (!UserValidate.CheckQuery())
                {
                    Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                    Response.Redirect("login.aspx");
                    Response.End();
                }
                else
                {
                    isquery = true;
                }
            }
            if (isquery)
            {
                studentid = int.Parse(HttpContext.Current.Session["QueryStudentId"] as string);
            }
            else if (!PageValidate.QueryStringIsEmpty("studentid"))
            {
                try
                {
                    studentid = int.Parse(PageValidate.QueryString("studentid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                IList<StudentScoreExtendInfo> infos = new List<StudentScoreExtendInfo>();
                infos = StudentScoreExtend.GetList(examroomid, 0, 0, 0, 0, 0, 0, 0);
                StudentScoreExtendInfo myInfo = infos.Single(t => t.StudentId == studentid);
                IList<KnowledgeInfo> listKnowledge = GetKnowledgeMode(myInfo.SubjectId);
                IList<QuestionInfo> listQuestion = GetQuestion(myInfo.ExamPaperId);

                StringBuilder sb = new StringBuilder();
                int number = 1;
                foreach (KnowledgeInfo know in listKnowledge)
                {
                    decimal questScoreCount = 0;
                    IList<int> questIds = new List<int>();
                    foreach (QuestionInfo quest in listQuestion)
                    {
                        if (quest.KnowId == know.Id)
                        {
                            questScoreCount += quest.Score;
                            questIds.Add(quest.Id);

                        }
                        else if (know.ChildKnowledgeInfos.Count > 0)
                        {
                            IList<int> ids = know.ChildKnowledgeInfos.Select(t=>t.Id).ToList();
                            if (GetArray<int>.Get(ids).Contains(quest.KnowId))
                            {
                                questScoreCount += quest.Score;
                                questIds.Add(quest.Id);
                            }
                            else
                            {
                                foreach (KnowledgeInfo n in know.ChildKnowledgeInfos)
                                {
                                    if (n.ChildKnowledgeInfos.Count > 0)
                                    {
                                        IList<int> childids = n.ChildKnowledgeInfos.Select(t => t.Id).ToList();
                                        if (GetArray<int>.Get(childids).Contains(quest.KnowId))
                                        {
                                            questScoreCount += quest.Score;
                                            questIds.Add(quest.Id);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (questIds.Count == 0)
                    {
                        continue;
                    }
                    string[] arrScore = GetClassScoreByQuest(infos, myInfo.StudentId, myInfo.ClassId, myInfo.GradeId, myInfo.AreaId, questIds);
                    string percentMy = "0";
                    string percentClass = "0";
                    string percentGrade = "0";
                    string percentArea = "0";
                    if (arrScore[0] != "0")
                    {
                        percentMy = Math.Round(decimal.Parse(arrScore[0]) / questScoreCount * 100).ToString() + "%";
                        mychart += Math.Round(decimal.Parse(arrScore[0]) / questScoreCount * 100).ToString() + ",";
                    }
                    else
                    {
                        mychart += "0,";
                    }
                    if (arrScore[1] != "0")
                    {
                        percentClass = Math.Round(decimal.Parse(arrScore[1]) / (questScoreCount * int.Parse(arrScore[4])) * 100).ToString() + "%";
                        classchart += Math.Round(decimal.Parse(arrScore[1]) / (questScoreCount * int.Parse(arrScore[4])) * 100).ToString() + ",";
                    }
                    else
                    {
                        classchart += "0,";
                    }
                    if (arrScore[2] != "0")
                    {
                        percentGrade = Math.Round(decimal.Parse(arrScore[2]) / (questScoreCount * int.Parse(arrScore[5])) * 100).ToString() + "%";
                        gradechart += Math.Round(decimal.Parse(arrScore[2]) / (questScoreCount * int.Parse(arrScore[5])) * 100).ToString() + ",";
                    }
                    else
                    {
                        gradechart += "0,";
                    }
                    if (arrScore[3] != "0")
                    {
                        percentArea = Math.Round(decimal.Parse(arrScore[3]) / (questScoreCount * int.Parse(arrScore[6])) * 100).ToString() + "%";
                        areachart += Math.Round(decimal.Parse(arrScore[3]) / (questScoreCount * int.Parse(arrScore[6])) * 100).ToString() + ",";
                    }
                    else
                    {
                        areachart += "0,";
                    }
                    sb.Append("<tr>");
                    sb.Append("<td>");
                    sb.Append(number);
                    sb.Append("</td>");
                    sb.Append("<td class='text_left'>");
                    sb.Append(know.Name);
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(questIds.Count.ToString());
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(questScoreCount.ToString());
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(arrScore[0]);
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(percentMy);
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(percentClass);
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(percentGrade);
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append(percentArea);
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    number++;
                }
                this.KnowledgeList.InnerHtml = sb.ToString();
                mychart = PageValidate.IsEmpty(mychart) ? "" : mychart.Substring(0, mychart.Length - 1);
                classchart = PageValidate.IsEmpty(classchart) ? "" : classchart.Substring(0, classchart.Length - 1);
                gradechart = PageValidate.IsEmpty(gradechart) ? "" : gradechart.Substring(0, gradechart.Length - 1);
                areachart = PageValidate.IsEmpty(areachart) ? "" : areachart.Substring(0, areachart.Length - 1);
            }
        }


        private string[] GetClassScoreByQuest(IList<StudentScoreExtendInfo> infos,int sid, int classid,int gradeid,int areaid, IList<int> questid)
        {
            decimal myScore = 0, classScore = 0, gradeScore = 0, areaScore = 0; 
            int classNum = 0, gradeNum = 0, areaNum = 0;
            foreach (StudentScoreExtendInfo info in infos)
            {
                IList<ScoreInfo> scoreInfos = ScoreHelper.GetScoreList(info.QuestItemScore);
                scoreInfos = scoreInfos.Where(t => GetArray<int>.Get(questid).Contains(t.Id)).ToList();
                decimal score = 0;
                foreach (ScoreInfo scoreInfo in scoreInfos)
                {
                    if (!PageValidate.IsEmpty(scoreInfo.Score))
                    {
                        score += decimal.Parse(scoreInfo.Score);
                    }
                }
                if (info.StudentId == sid)
                {
                    myScore = score;
                }
                if (info.ClassId == classid)
                {
                    classScore += score;
                    classNum++;
                }
                if (info.GradeId == gradeid)
                {
                    gradeScore += score;
                    gradeNum++;
                }
                if (info.AreaId == areaid)
                {
                    areaScore += score;
                    areaNum++;
                }
            }
            return new string[] { myScore.ToString(), classScore.ToString(), gradeScore.ToString(), areaScore.ToString(), classNum.ToString(), gradeNum.ToString(), areaNum.ToString() };
        }
        private IList<KnowledgeInfo> GetKnowledgeMode(int subjectid)
        {
            IList<KnowledgeInfo> infos = new List<KnowledgeInfo>();
            infos = Knowledge.GetKnowledgeInfosBySubjectId(subjectid).Where(t => t.Keyword.Length == 1).ToList();
            foreach (KnowledgeInfo info in infos)
            {
                info.ChildKnowledgeInfos = Knowledge.GetKnowledgeInfos(info.Id);
                foreach (KnowledgeInfo subInfo in info.ChildKnowledgeInfos)
                {
                    subInfo.ChildKnowledgeInfos = Knowledge.GetKnowledgeInfos(subInfo.Id);
                }
            }
            return infos;
        }

        private IList<QuestionInfo> GetQuestion(int exampaperid)
        {
            IList<QuestionInfo> infos = new List<QuestionInfo>();
            infos = Question.GetQuestions(exampaperid, "", "", "", "", "", "", "", "", "", 0, 0);
            return infos;
        }
    }
}
